package com.xiaohe66.web.application.im;

import com.xiaohe66.common.util.IdWorker;
import com.xiaohe66.common.web.sec.annotation.NeedLogin;
import com.xiaohe66.common.web.sec.annotation.NeedRoleAll;
import com.xiaohe66.common.web.validator.annotation.Verify;
import com.xiaohe66.web.application.im.bo.ImUserUpdateBo;
import com.xiaohe66.web.application.im.bo.ImUserUploadKeyBo;
import com.xiaohe66.web.application.im.convert.ImUserBoConverter;
import com.xiaohe66.web.application.im.result.ImUserResult;
import com.xiaohe66.web.domain.account.aggregate.Account;
import com.xiaohe66.web.domain.account.repository.AccountRepository;
import com.xiaohe66.web.domain.im.agg.ImUser;
import com.xiaohe66.web.domain.im.repository.ImUserRepository;
import com.xiaohe66.web.domain.im.service.ImUserService;
import com.xiaohe66.web.domain.im.value.ImUserId;
import com.xiaohe66.web.domain.im.value.ImUserNickname;
import com.xiaohe66.web.domain.im.value.ImUserPublicKey;
import com.xiaohe66.web.domain.im.value.ImUserSignature;
import com.xiaohe66.web.domain.sys.sec.entity.CurrentAccount;
import com.xiaohe66.web.domain.sys.sec.service.SecurityService;
import com.xiaohe66.web.domain.sys.sec.value.RoleId;
import com.xiaohe66.web.domain.sys.sec.value.RoleName;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaohe
 * @since 2023.09.07 16:31
 */
@Service
@RequiredArgsConstructor
public class ImUserAppService {

    private final ImUserBoConverter boConverter;
    private final ImUserRepository imUserRepository;
    private final AccountRepository accountRepository;
    private final ImUserService imUserService;
    private final SecurityService securityService;


    public void update(ImUserUpdateBo bo) {
        // TODO : 实现更新用户名
    }

    @Verify
    @NeedRoleAll(RoleName.IM_ROLE_VALUE)
    public void uploadPublicKey(ImUserUploadKeyBo bo) {

        ImUserPublicKey publicKey = new ImUserPublicKey(bo.getPublicKey());

        imUserService.changeCurrentImUserPublicKey(publicKey);
    }

    @NeedLogin
    public ImUserResult currentUserInfo() {

        ImUser imUser = imUserService.getByCurrentUser();

        if (imUser == null) {

            // TODO : 当前是默认创建一个用户，未来应改成注册

            CurrentAccount currentAccount = securityService.getCurrentAccount();

            ImUserId id = new ImUserId(IdWorker.genId());

            imUser = ImUser.builder()
                    .id(id)
                    .createId(currentAccount.getId())
                    .nickname(new ImUserNickname(currentAccount.getName().getValue()))
                    .signature(ImUserSignature.EMPTY)
                    .build();

            imUserRepository.save(imUser);

            // 添加角色
            Account account = accountRepository.getById(currentAccount.getId());
            account.addRole(RoleId.IM_ROLE_ID);
            accountRepository.save(account);

            currentAccount.addRole(RoleName.IM_ROLE_VALUE);
        }

        return boConverter.toResult(imUser);
    }

    @NeedRoleAll(RoleName.IM_ROLE_VALUE)
    public List<ImUserResult> listAll() {

        List<ImUser> list = imUserRepository.listAll();

        return boConverter.toResult(list);
    }
}
